/* Copyright 2009 Requirements Management System
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view;

import java.util.List; 
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.mags.common.EngineContext;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.ViewType;
import org.mags.remas.view.commons.RelationshipViewBase;
import org.mags.remas.view.widgets.RelationshipTreeViewer;
import org.mags.remas.view.widgets.RelationshipTreeViewerListener;
import org.mags.remas.view.widgets.TreeActionTransmitter;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class AbstractEntityView extends RelationshipViewBase 
	implements RelationshipTreeViewerListener, ICellModifier {
	private RelationshipTreeViewer treeViewer;
	private CellEditor[] editors = new CellEditor[1];
	protected ViewType viewType = EngineContext.getDataContext().getEntityProviderFactory().getViewTypeProvider().getTREEVIEW();
	
	public AbstractEntityView() {
	}
	public AbstractEntityView(ViewType viewType) {
		this.viewType = viewType;
	}
	@Override
	public void dispose() {
		super.dispose();
		if(treeViewer!=null)
			TreeActionTransmitter.getInstance().remove(treeViewer);
	}
	public RelationshipTreeViewer getTreeViewer() {
		return treeViewer;
	}
	public void setTreeViewer(RelationshipTreeViewer treeViewer) {
		if(this.treeViewer!=null)
			throw new RuntimeException("tree viewer is not dispose");
		if(treeViewer==null)
			throw new RuntimeException("tree viewer can't be null");
		this.treeViewer = treeViewer;
	}	
	@Override
	public void createPartControl(Composite parent) {
		treeViewer = new RelationshipTreeViewer(parent, SWT.NONE, viewType);
		TreeActionTransmitter.getInstance().add(treeViewer);
		treeViewer.addRelationshipTreeViewerListener(this);
		treeViewer.setColumnProperties(new String[]{"name"});
		treeViewer.setCellModifier(this);
		treeViewer.getControl().setToolTipText("F2 to edit and ENTER to save changes");
		editors[0] = new TextCellEditor(treeViewer.getTree());
		treeViewer.setCellEditors(editors);
		super.getSite().setSelectionProvider(treeViewer);		
	}
	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
	public abstract boolean dragAndDrop(EntityRelationship target, List<EntityRelationship> elementsDroped);
	public abstract void doubleClick(EntityRelationship first);
	public abstract void update(EntityRelationship entityRelationship, String property, Object value);	
	public void select(EntityRelationship first, List<EntityRelationship> elements) {
		PropertiesView propertiesView = (PropertiesView)getSite().getPage().findView(PropertiesView.ID);
		if(propertiesView!=null) {
			propertiesView.setEntityRelationship(first);
			propertiesView.refresh();					
		}				
	}
	@Override
	public boolean canModify(Object element, String property) {
		return true;
	}
	@Override
	public Object getValue(Object element, String property) {
		return ((EntityRelationship)element).getEntity().getName();
	}
	@Override
	public void modify(Object element, String property, Object value) {
		TreeItem treeItem = (TreeItem)element;
		EntityRelationship entityRelationship = (EntityRelationship)treeItem.getData();
		if(value!=null&&((String)value).equals(entityRelationship.getEntity().getName()))
			return ;
		update(entityRelationship, property, value);	
	}		
}
